<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>贪吃蛇游戏</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }

        #game-board {
            display: grid;
            grid-template-columns: repeat(20, 20px);
            grid-template-rows: repeat(20, 20px);
            gap: 1px;
            background-color: #333;
        }

        .snake {
            background-color: #0f0;
        }

        .food {
            background-color: #f00;
        }
    </style>
</head>

<body>
    <div id="game-board"></div>
    <script>
        const board = document.getElementById('game-board');
        const boardSize = 20;
        let snake = [{ x: 10, y: 10 }];
        let food = { x: Math.floor(Math.random() * boardSize), y: Math.floor(Math.random() * boardSize) };
        let direction = 'right';
        let gameInterval;

        function draw() {
            board.innerHTML = '';
            snake.forEach(segment => {
                const snakeElement = document.createElement('div');
                snakeElement.classList.add('snake');
                snakeElement.style.gridColumn = segment.x + 1;
                snakeElement.style.gridRow = segment.y + 1;
                board.appendChild(snakeElement);
            });

            const foodElement = document.createElement('div');
            foodElement.classList.add('food');
            foodElement.style.gridColumn = food.x + 1;
            foodElement.style.gridRow = food.y + 1;
            board.appendChild(foodElement);
        }

        function move() {
            const head = { ...snake[0] };
            switch (direction) {
                case 'up':
                    head.y--;
                    break;
                case 'down':
                    head.y++;
                    break;
                case 'left':
                    head.x--;
                    break;
                case 'right':
                    head.x++;
                    break;
            }

            snake.unshift(head);

            if (head.x === food.x && head.y === food.y) {
                food = { x: Math.floor(Math.random() * boardSize), y: Math.floor(Math.random() * boardSize) };
            } else {
                snake.pop();
            }

            if (head.x < 0 || head.x >= boardSize || head.y < 0 || head.y >= boardSize) {
                clearInterval(gameInterval);
                alert('游戏结束！');
            }

            for (let i = 1; i < snake.length; i++) {
                if (head.x === snake[i].x && head.y === snake[i].y) {
                    clearInterval(gameInterval);
                    alert('游戏结束！');
                }
            }
        }

        function changeDirection(event) {
            const key = event.key;
            if (key === 'ArrowUp' && direction !== 'down') {
                direction = 'up';
            } else if (key === 'ArrowDown' && direction !== 'up') {
                direction = 'down';
            } else if (key === 'ArrowLeft' && direction !== 'right') {
                direction = 'left';
            } else if (key === 'ArrowRight' && direction !== 'left') {
                direction = 'right';
            }
        }

        function startGame() {
            draw();
            gameInterval = setInterval(() => {
                move();
                draw();
            }, 200);
        }

        document.addEventListener('keydown', changeDirection);
        startGame();
    </script>
</body>

</html>